package com.hbx.staservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hbx.commonutils.R;
import com.hbx.staservice.client.UcenterClient;
import com.hbx.staservice.entity.StatisticsDaily;
import com.hbx.staservice.mapper.StatisticsDailyMapper;
import com.hbx.staservice.service.StatisticsDailyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author hbx
 * @since 2023-10-22
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {

    @Autowired
    private UcenterClient ucenterClient;

    @Override
    public void registerCount(String day) {
        //添加记录之前先删除相同日期的数据
        LambdaQueryWrapper<StatisticsDaily> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StatisticsDaily::getDateCalculated,day);
        this.remove(queryWrapper);
        //远程调用得到一天的注册人数
        R r = ucenterClient.countRegister(day);
        Integer countRegister = (Integer) r.getData().get("countRegister");

        //把获取的数据添加到数据库，统计分析表里面
        StatisticsDaily sta = new StatisticsDaily();
        sta.setRegisterNum(countRegister);//注册人数
        sta.setDateCalculated(day);//统计日期

        sta.setVideoViewNum(RandomUtils.nextInt(100,200));
        sta.setLoginNum(RandomUtils.nextInt(100,200));
        sta.setCourseNum(RandomUtils.nextInt(100,200));
        this.save(sta);
    }

    @Override
    public Map<String, Object> getShowData(String type, String begin, String end) {
        QueryWrapper<StatisticsDaily> queryWrapper = new QueryWrapper<>();
        queryWrapper.between("date_calculated",begin,end);
        queryWrapper.select("date_calculated",type);
        List<StatisticsDaily> list = this.list(queryWrapper);

        //因为返回两部分数据， 日期 和 日期对应的数量
        //前端要求返回json数组格式
        ArrayList<String> date_calculatedList = new ArrayList<>();
        ArrayList<Integer> numDataList = new ArrayList<>();

        for (int i = 0; i < list.size(); i++) {
            StatisticsDaily statisticsDaily = list.get(i);
            //封装日期
            date_calculatedList.add(statisticsDaily.getDateCalculated());
            //封装数量
            if(type.equals("login_num")){
                numDataList.add(statisticsDaily.getLoginNum());
            }else if(type.equals("register_num")){
                numDataList.add(statisticsDaily.getRegisterNum());
            }else if(type.equals("video_view_num")){
                numDataList.add(statisticsDaily.getVideoViewNum());
            }else if(type.equals("course_num")){
                numDataList.add(statisticsDaily.getCourseNum());
            }
        }

        Map<String,Object> map = new HashMap<>();
        map.put("date_calculatedList",date_calculatedList);
        map.put("numDataList",numDataList);

        return map;
    }
}
